/*******************************************************************
 * author   :  Charlies-ls
 * Date     :  2021-09-26
 * Function :  thread utils
 * Copyright (c) 2021. artestplay@outlook.com. All Rights Reserved.
 *******************************************************************/

#ifndef AX_THREAD_H__
#define AX_THREAD_H__

#include <stdio.h>
#include <pthread.h>
#include <stdbool.h>

typedef pthread_t       ax_thread_t;
typedef pthread_mutex_t ax_mutex_t;
typedef pthread_cond_t  ax_cond_t;
#define INVALIDATE_THREAD(t) (t==-1)

#ifdef __cplusplus
extern "C" {
#endif

ax_mutex_t* ax_mutex_create();
void        ax_mutex_destroy(ax_mutex_t*m);
bool        ax_mutex_lock(ax_mutex_t* m);
bool        ax_mutex_unlock(ax_mutex_t*m);
bool        ax_ax_mutex_try_lock(ax_mutex_t*m);

ax_cond_t* ax_cond_create();
void       ax_cond_destroy(ax_cond_t*m);
bool       ax_cond_wait(ax_cond_t*cond, ax_mutex_t*mutex);
bool       ax_cond_wait_timeout(ax_cond_t*cond, ax_mutex_t*mutex, double seconds);
bool       ax_cond_wait_timeout_ms(ax_cond_t*cond,ax_mutex_t*mutex,double ns);
bool       ax_cond_signal(ax_cond_t* cond);


typedef void* (*ax_thread_fun)(void*data);
ax_thread_t ax_thread_start(ax_thread_fun fun, void*param);
bool        ax_thread_wait(ax_thread_t* thread);
bool        ax_thread_cancel(ax_thread_t* t);
bool        ax_thread_is_cancel(ax_thread_t*t);

#ifdef __cplusplus
}
#endif

#endif
